package com.wuchao.servlet143;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mysql.cj.xdevapi.JsonArray;
import com.wuchao.entity.Pojo;
import com.wuchao.service.serviceimpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/26 16:25
 */
@WebServlet(name = "ServletAjax2",urlPatterns = "/ServletAjax2")
public class ServletAjax2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();

        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();
        String op =request.getParameter("op");
        if(op.equals("login")){
            String text = request.getParameter("name");
            int loading = new ServiceImpl().isLoading("select count(1) from t143 where name=?", text);
            System.out.println(loading);
            if(loading>0){
                out.println("y");//异步就是我送你回去，想不到吧，曾经的我默默无闻。现在最难的还是他出马
            }else{
                out.println("n");
            }


        }else  if(op.equals("xuliehua")){
            String name= request.getParameter("name");
            int loading = new ServiceImpl().isLoading("select count(1) from t143 where name=?", name);
            System.out.println(loading);
            if(loading>0){
                out.println("n");//异步就是我送你回去，想不到吧，曾经的我默默无闻。现在最难的还是他出马
            }else{
                String sex= request.getParameter("sex");
                int age=Integer.parseInt(request.getParameter("age"));
                new ServiceImpl().update("insert t143(name,sex,age) values(?,?,?)",name,sex,age);
                out.println("y");
            }

        }else  if(op.equals("find1")){
            String name= request.getParameter("text1");
            List<Pojo> poJo = new ServiceImpl().findPoJo("select * from t143 where name like ?", "%" + name + "%");
            String s = JSON.toJSONString(poJo);
            out.println(s);

        }




        else  if(op.equals("login1")){
            String text = request.getParameter("text");
            int loading = new ServiceImpl().isLoading("select count(1) from t143 where name=?", text);
            System.out.println(loading);
            if(loading>0){
                out.println("y");//异步就是我送你回去，想不到吧，曾经的我默默无闻。现在最难的还是他出马
            }else{
                out.println("n");
            }

        }else  if(op.equals("find")){//redis --非关系型数据库--缓存
            String text = request.getParameter("user");
            List<Pojo> poJo = new ServiceImpl().findPoJo("select * from t143 where name like ?", "%" + text + "%");
            String s = JSON.toJSONString(poJo);
            //JSON.parseObject() 解析拿值,
           /* String s1=JSON.toJSONStringWithDateFormat(poJo,"yyyy-MM-dd HH:mm:ss",
                    SerializerFeature.NotWriteDefaultValue,SerializerFeature.WriteNullNumberAsZero);*/
            System.out.println(s);
           out.println(s);
        }


        out.flush();
        out.close();
    }
}
